<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/19
 * Time: 10:12
 */

namespace zhtfrontend\controllers;

use common\models\datas\Brand;
use common\models\datas\Cate;
use common\models\datas\CateAttr;
use common\models\datas\Type;
use yii\widgets\ActiveForm;
use zhtfrontend\components\bases\BaseController;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\Coupons;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\web\BadRequestHttpException;
use yii\helpers\ArrayHelper;
use yii\data\Pagination;
use yii\helpers\Json;
use zhtfrontend\models\datas\CustomerCoupons;
use zhtfrontend\models\datas\CustomerCouponsLog;

class CustomercouponsController extends BaseController
{
    // public $layout='@app/views/layouts/iframe.php';

    /**
     * 会员领取优惠券列表
     */
    public function actionIndex()
    {
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $customer_coupons = CustomerCoupons::find()->orderBy('id DESC');
//            if (!empty($param['searchData'])) {
//                $search_data = array_filter($param['searchData']);
//            }
        $pages = new Pagination(['totalCount' => $customer_coupons->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $customer_coupons = $customer_coupons->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($customer_coupons, ['operate', 'customer_name', 'coupons_name', 'coupons_code']);

        return $this->render('index', ['data' => $array, 'pages' => $pages]);
    }

    /**
     * 延迟失效时间
     */
    public function actionDelay()
    {
        $this->layout = '@app/views/layouts/iframe.php';
        $id = \Yii::$app->request->get('id');
        //判断该券是否失效被领取
        $model = CustomerCoupons::findOne(['id' => $id]);
        //ajax验证
        if (\Yii::$app->request->isAjax && \Yii::$app->request->post('ajax') == 'w0' && $model->load(\Yii::$app->request->post())) {
            \Yii::$app->response->format = Response::FORMAT_JSON;
            return ActiveForm::validate($model);
        }
        if ($model->load(\Yii::$app->request->post()) && $model->validate()) {
            $post = \Yii::$app->request->post('CustomerCoupons');
            $orther = CustomerCoupons::find()
                ->where(['customer_id' => $model->customer_id, 'coupons_code_id' => $model->coupons_code_id])
                ->andWhere(['!=', 'id', $id])
                ->all();
            if (isset($orther) && empty($orther)) {
                //优惠券没被领取
                //判断该优惠券是否延时过
                $one = CustomerCouponsLog::find()->where(['customer_coupons_id' => $id])->all();
                if (isset($one) && empty($one)) {
                    //没有被延时过
                    $invalid_time = $model->invalid_time;
                    $model->invalid_time = $invalid_time + $post['day'] * 86400;
                    if ($model->save(false)) {
                        $customer_coupons_log = new CustomerCouponsLog;
                        $customer_coupons_log->customer_coupons_id = $id;
                        $customer_coupons_log->coupon_id = $model->coupon_id;
                        $customer_coupons_log->customer_id = $model->customer_id;
                        $customer_coupons_log->coupons_code_id = $model->coupons_code_id;
                        $customer_coupons_log->invalid_time = $invalid_time;
                        $customer_coupons_log->delay_invalid_time = $model->invalid_time;
                        $customer_coupons_log->user_id = \Yii::$app->user->id;
                        $customer_coupons_log->create_at = time();
                        if ($customer_coupons_log->save(false)) {
                            return $this->redirectMessage('延时成功', Fun::url(['customercoupons/index']));
                        } else {
                            return $this->redirectMessage('延时失败', Fun::url(['customercoupons/index']));
                        }
                    }
                } else {
                    return $this->redirectMessage('该优惠券已延时一次', Fun::url(['customercoupons/index']));
                }
            } else {
                return $this->redirectMessage('该优惠券已被领取', Fun::url(['customercoupons/index']));
            }
        }
        return $this->render('delay', ['model' => $model]);
    }

}